package controll;

import java.io.IOException;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import swimv2.entity.User;
import swimv2.session.ILoginRemote;
import swimv2.session.ISearchModuleRemote;
import swimv2.session.SearchParser;
import utils.SCmd;
import utils.SeU;

/**
 * Servlet implementation class Search
 */
public class Search extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Search() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.process(request,response);
	}

	private void process(HttpServletRequest request,HttpServletResponse response) {
HttpSession session = request.getSession();
		
		String next = "my_profile.jsp";
		
		ILoginRemote log = (ILoginRemote)session.getAttribute(SeU.LOGIN_BEAN_ATTRIBUTE);
		if(log!=null)
		{
			//session.setAttribute(SeU.STATUS_ATTRIBUTE, "user");
			User u = (User)session.getAttribute(SeU.USER_ATTRIBUTE);
			
			String command = request.getParameter(SCmd.SERVLET_COMMAND);
			if(command!=null)
			{
				try {
					InitialContext ctx = new InitialContext();
					if (command.equals(SCmd.SHOW_RESULT)) {
						String s = request.getParameter(SeU.SEARCH_ATTRIBUTE);
						ISearchModuleRemote sm =(ISearchModuleRemote)ctx
								.lookup("ISearchModule");
						
						List<User> user = sm.getUsersParametric(SearchParser.parse(s));
						
						next=response.encodeURL("search_master.jsp");
					}
					else if(command.equals(SCmd.SHOW_RESULT))
					{
						ISearchModuleRemote sm =(ISearchModuleRemote)ctx
								.lookup("ISearchModule");
						User us = sm.getUser(Integer.parseInt(request.getParameter(SeU.USER_ATTRIBUTE)));
						
						next=response.encodeURL("search_detail.jsp");
					}
				} catch (NamingException e) {
					// TODO: handle exception
				}
				prepareOutput(session, u, request);
				//DISPATCHER
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(response.encodeURL(next));
				
				if (dispatcher!=null)
					try {
						dispatcher.forward(request, response);
					} catch (ServletException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
			else
			{}
		}
		else
		{
			try {
				response.sendRedirect(response.encodeURL("Home"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
		
	}

	private void prepareOutput(HttpSession session, User u,HttpServletRequest request) {
		// TODO Auto-generated method stub
		
	}

	private void commandAnalisis(HttpServletResponse response,
			HttpServletRequest request, User u, String command) {
		// TODO Auto-generated method stub
		
	}

}
